package com.md.rps.service;

import com.md.rps.mybatis.interfaces.*;
import com.md.rps.mybatis.model.USER;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserOperator {

    private I_USER i_user;

    public USER getUserByID(int id) {
        return i_user.getUser(id);
    }

    public USER getUserByWeChart(String openid, String unionid) {
        return i_user.getUserByWeChart(openid, unionid);
    }

    public USER getUserByCard(String CARD_NUM){
        return i_user.getUserByCard(CARD_NUM);
    }

    public List<USER> getUserList(int start, int end){
        return i_user.getUserList(start,end);
    }

    public int addUser(USER user) {
        int result = 0; // 状态码，0表示注册失败
        if (i_user.getUserByCard(user.getCARD_NUM()) != null) {
            result = 2; // 2表示卡号已经被注册
        }else if (i_user.getUserByWeChart(user.getOPENID(),user.getUNIONID()) != null) {
            result = 3; // 3表示该微信号被注册
        }else {
            user.setID(null);
            result = i_user.addUser(user);// 成功应该返回1
        }
        return result;
    }

    public int updateUser(USER user){
        return i_user.updateUser(user);
    }

    public int deleteUser(int id){
        return i_user.deleteUser(id);
    }
}
